﻿// Created by Windward Studios - no copyright is claimed. This code can be used in
// any manner by anyone for any reason. There is no copyright of any kind on it. You may
// use it in commercial products. You may change it without sharing those changes.
// We ask that you keep the "created by Windward Studios" in a comment at the top.

using System.Data.Common;
using Kailua.net.windward.utils;

namespace net.windward.utils.ado.Unknown
{
	///<summary>
	/// A lightweight handler of functionality that the ODBC and OleDb drivers can use as well as the main implementations.
	/// This class is for drivers we do not have anything specific written for.
	///</summary>
	class WrUnknownSyntax : IWrSyntax
	{

		private readonly DbProviderFactory providerFactory;
		private readonly string odbcProvider;

		/// <summary>
		/// Create the object.
		/// </summary>
		/// <param name="providerFactory">The provider for this connector.</param>
		/// <param name="odbcProvider">The ODBC Provider used.</param>
		public WrUnknownSyntax(DbProviderFactory providerFactory, string odbcProvider)
		{
			this.providerFactory = providerFactory;
			this.odbcProvider = odbcProvider;
		}

		/// <summary>
		/// A connection string for a database on a server.
		/// </summary>
		/// <param name="server">The server to access (ex: localhost).</param>
		/// <param name="database">The database to connect to.</param>
		/// <param name="credentials">The user credentials to access the database.</param>
		/// <returns>The connection string for this database on this server.</returns>
		public string ConnectionString(string server, string database, WrCredentials credentials)
		{

			Trap.trap();
			DbConnectionStringBuilder connStrBuilder = providerFactory.CreateConnectionStringBuilder();
			if (! string.IsNullOrEmpty(odbcProvider))
				connStrBuilder.Add("Driver", odbcProvider);

			// seems most common
			if ((string.IsNullOrEmpty(database)) && (server.IndexOf('.') != -1))
			{
				Trap.trap();
				if (!string.IsNullOrEmpty(server))
					connStrBuilder.Add("Server", server);
				connStrBuilder.Add("AttachDbFilename", FileUtils.FullPath(server));
			}
			else
			{
				connStrBuilder.Add("Server", server);
				if (!string.IsNullOrEmpty(database))
					connStrBuilder.Add("Database", database);
			}
			if (credentials.UseWindowsIdentity)
				connStrBuilder.Add("Trusted_Connection", "yes");
			else
			{
				connStrBuilder.Add("Uid", credentials.Username);
				connStrBuilder.Add("Pwd", credentials.Password);
			}
			return connStrBuilder.ConnectionString;
		}

		/// <summary>
		/// Convert a select to normalize the "top 10" syntax
		/// </summary>
		/// <param name="select">The select with our standard usage</param>
		/// <returns>The select for this database.</returns>
		public string ConvertSelect(string select)
		{
			return select;
		}

		/// <summary>
		/// Create a safe table name. Generally needed only if the name has a space in it or is a reserved name.
		/// </summary>
		/// <param name="tableName">The true table name.</param>
		/// <returns>The table name as it should be used in a select.</returns>
		public string SafeTableName(string tableName)
		{
			return tableName;
		}

		/// <summary>
		/// Create a safe column name. Generally needed only if the name has a space in it or is a reserved name.
		/// </summary>
		/// <param name="columnName">The true column name.</param>
		/// <returns>The table name as it should be used in a select.</returns>
		public string SafeColumnName(string columnName)
		{
			return columnName;
		}

		/// <summary>
		/// Create a safe table name. Generally needed only if the name has a space in it or is a reserved name.
		/// </summary>
		/// <param name="tableName">The true table name.</param>
		/// <param name="columnName">The true column name.</param>
		/// <returns>The table.column name as it should be used in a select.</returns>
		public string SafeTableColumnName(string tableName, string columnName)
		{
			return tableName + '.' + columnName;
		}
	}
}